<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <!-- 在vue3中都可以使用计算属性来代替过滤器，所以把过滤器删了 -->
      <!-- 也可以使用methods或者自定义指令等方式来处理 -->
      <h2>vue3解决过滤器</h2>
      <!-- <p>性别: {{ fn(sex) }}</p> -->
      <p>性别: {{ sex2 }}</p>
      <p>当前事件: {{ now }}</p>
      <ul>
        <li v-for="item in list" :key="item.name">
          {{ item.name }}的出生日期是<span v-time="item.birthday"></span>
        </li>
      </ul>
    </div>

    <script src="./lib/vue3.js"></script>
    <script>
      const { createApp } = Vue;

      createApp({
        data() {
          return {
            sex: 1,
            time: 1684218547924,
            list: [
              {
                name: "zhangsan",
                birthday: 1684218047924,
              },
              {
                name: "lisi",
                birthday: 1684217047924,
              },
            ],
          };
        },
        directives: {
          time(el, binding) {
            let time = new Date(binding.value);
            let year = time.getFullYear();
            let month = time.getMonth() + 1;
            let date = time.getDate();
            let hours = time.getHours();
            let minute = time.getMinutes();
            let second = time.getSeconds();

            if (month < 10) {
              month = "0" + month;
            }
            if (date < 10) {
              date = "0" + date;
            }
            if (hours < 10) {
              hours = "0" + hours;
            }
            if (minute < 10) {
              minute = "0" + minute;
            }
            if (second < 10) {
              second = "0" + second;
            }
            el.innerHTML =
              year +
              "-" +
              month +
              "-" +
              date +
              " " +
              hours +
              ":" +
              minute +
              ":" +
              second;
          },
        },
        // methods: {
        //   fn(sex) {
        //     return sex === 1 ? "男" : "女";
        //   },
        // },
        computed: {
          sex2() {
            return this.sex === 1 ? "男" : "女";
          },
          now() {
            let time = new Date(this.time);
            let year = time.getFullYear();
            let month = time.getMonth() + 1;
            let date = time.getDate();
            let hours = time.getHours();
            let minute = time.getMinutes();
            let second = time.getSeconds();

            if (month < 10) {
              month = "0" + month;
            }
            if (date < 10) {
              date = "0" + date;
            }
            if (hours < 10) {
              hours = "0" + hours;
            }
            if (minute < 10) {
              minute = "0" + minute;
            }
            if (second < 10) {
              second = "0" + second;
            }
            return (
              year +
              "-" +
              month +
              "-" +
              date +
              " " +
              hours +
              ":" +
              minute +
              ":" +
              second
            );
          },
        },
      }).mount("#app");
    </script>
  </body>
</html>
